package com.itheima.service.impl;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByTelephone(telephone);
        return member;
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            //加密
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        try {
            memberDao.add(member);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //根据月份查找会员出现的次数
    @Override
    public Result findMemberCountByMonth() {
        //日期格式化
        SimpleDateFormat sim = new SimpleDateFormat("yyyy.MM");

        Calendar calendar = Calendar.getInstance();
        //System.out.println("初始化实例:" + sim.format(calendar.getTime()));
        //获取当前月份之前的12个月份 (获得当前日期之前12个月的日期)
        calendar.add(Calendar.MONTH, -12);
        //System.out.println("获取当前日期之前12个月的日期:" + sim.format(calendar.getTime()));
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            //System.out.println("前12个月的日期 (++) 第" + (i+1) + "次:" + sim.format(calendar.getTime()));
            //添加到集合
            list.add(sim.format(calendar.getTime()));
        }

        Map<String, Object> map = new HashMap<>();
        //第一个值(months)
        map.put("months",list);

        List<Integer> memberCountList = new ArrayList<>();
        //根据月份查询每月的会员数量
        for (String month : list) {
            month = month + ".31";
            Integer memberCount = memberDao.findMemberCountByMonths(month);
            memberCountList.add(memberCount);
        }
        map.put("memberCount",memberCountList);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    public static void main(String[] args) {
       /* String a = "1.2.3";
        String s = a.replace(".", "-");
        System.out.println(s + a);*/
        Calendar calender = Calendar.getInstance();

        SimpleDateFormat sim = new SimpleDateFormat("yyyy.MM.dd");
        calender.set(Calendar.YEAR,2022);
        /*
         * 月份的值可以使用常量
         * 也可以使用定量，需要注意:月份从0开始
         * 即0表示1月
         */
        calender.set(Calendar.MONTH,1);
        calender.set(Calendar.DAY_OF_MONTH,1);

        calender.add(Calendar.MONTH,1);

        calender.add(Calendar.DAY_OF_MONTH,-1);

        System.out.println(sim.format(calender.getTime()));


       // Date date = calender.getTime();

        /*//日期格式化
        SimpleDateFormat sim = new SimpleDateFormat("yyyy.MM");

        Calendar calendar = Calendar.getInstance();
        System.out.println("初始化实例:" + sim.format(calendar.getTime()));
        //获取当前月份之前的12个月份 (获得当前日期之前12个月的日期)
        calendar.add(Calendar.MONTH, -12);
        System.out.println("获取当前日期之前12个月的日期:" + sim.format(calendar.getTime()));
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            System.out.println("前12个月的日期 (++) 第" + (i+1) + "次:" + sim.format(calendar.getTime()));
            //添加到集合
            list.add(sim.format(calendar.getTime()));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("months",list);

        //通过月份循环查询每月的会员个数*/



    }
}
